'''
Created on 2011-9-2

@author: Administrator
'''
import task
import logging

parallel_type_bagin,\
pt_global_index,\
pt_param_combin,\
parallel_type_end = range(4)



class MachineCondition():
    def __init__(self, machinelist = None, os = None, groupid = None, system = None):
        self.machinelist = machinelist
        self.os = os
        self.groupid = groupid
        self.system = system



class TaskTemplate():
    max_task = 5
    
    def __init__(self, id, machine_info, name, script_name, trigger, mailreceiver, args = None, next_run_time = None, scxml = None, 
                 success_receiver = None, fail_receiver = None, except_receiver = None):
        
        if not id:
            raise ValueError('The id must not be None ')
        if  trigger == None and next_run_time == None:
            raise ValueError('The trigger and next_run_time must not all be None ')
        
        if not name:
            raise ValueError('The name must not be None ')
        
        if not machine_info:
            raise ValueError('The machine_info must not be None ') 
        
        if  (script_name == None and scxml == None) or (script_name != None and scxml != None):
            raise ValueError('The run type  error')
        
        
        
        self.id  = id
        self.name = name
        
        if args != None:
            self.args = args
        else:
            self.args = ''
        self.machine_info = machine_info
        self.trigger = trigger 
        self.script_name = script_name
        self.next_run_time = next_run_time
        self.tasklist = set()
        self.is_parallel = False
        self.paralleltyep = None
        self.mailreciver = mailreceiver
        
        
        self.success_mail_receiver = success_receiver or ""
        self.error_mail_receiver = fail_receiver or ""
        self.except_mail_receiver = except_receiver or ""
        
        if script_name != None:
            self.type = 'script'
            self.run_info = {'scriptname':script_name}
        
        if scxml != None:
            self.type = 'scxml'
            self.run_info = {'xml':scxml}
        
    def GetNextRunTime(self):
        return self.next_run_time
    
    def SetNextRunTime(self, now):
        self.next_run_time = self.trigger.GetNextFireTime(now)
        
        
    def RemoveTask(self, id):
        if id not in self.tasklist:
            raise ValueError('id not in list')
        self.tasklist.remove(id)
        
    def AddTask(self, taskid):
        if taskid in self.tasklist:
            raise ValueError(' task id aleady in template')
        self.tasklist.add(taskid) 
        
    def CreateTask(self, tempid):
        if len(self.tasklist) > self.max_task:
            logging.info('template %s task cout more than max create fail' % self.id)
            return 
        new =  task.Task(tempid, self)
        self.tasklist.add(new.tempid)
        return new
    
class CaseTaskTemplate(TaskTemplate):
    def __init__(self, id, machine_info, name, script_name, trigger, casesql, mailreceiver, args = None, next_run_time = None, scxml = None,
                 success_receiver = None, fail_receiver = None, except_receiver = None):
        
        TaskTemplate.__init__(self, id, machine_info, name, script_name, trigger, mailreceiver, args, next_run_time, scxml,
                              success_receiver, fail_receiver, except_receiver)
        self.casesql = casesql
    
    
    def CreateTask(self, tempid):
        if len(self.tasklist) > self.max_task:
            logging.info('template %s task cout more than max create fail' % self.id)
            return 
        new =  task.CaseTask(tempid, self)
        self.tasklist.add(new.tempid)
        return new